// DataSourceManager.h: interface for the DataSourceManager class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_DATASOURCEMANAGER_H__0EE8FB56_78AB_4573_90B5_27E4605BE132__INCLUDED_)
#define AFX_DATASOURCEMANAGER_H__0EE8FB56_78AB_4573_90B5_27E4605BE132__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <afxtempl.h>
#include "PartFormatManager.h"

class DataSourceItem
{
public:
	DataSourceItem() { m_item = 0; }
	~DataSourceItem() {}

public:
	const CString& Uri() const { return m_item->Uri(); }
	const CString Path() const {
			int iPos = Uri().ReverseFind ( _T ( '\\' ) );
			if ( iPos == -1 )
				return "";
			return Uri().Left( iPos ); }
	const CString FileName() const {
			int iPos = Uri().ReverseFind ( _T ( '\\' ) );
			if ( iPos == -1 )
				return "";
			return Uri().Mid ( iPos + 1 ); }
	const CString& FormatName() const { return m_item->FormatName(); }
	const CString& QueryStatement() const { return m_QueryStatement; }
//	const CString& SpecKeyName() const { return m_item->SpecKeyName(); }
	_ConnectionPtr* Connection() const { return m_item->Connection(); }

	void ItemID(const CString& strID){
		m_strID = strID;
	}

	const CString& ItemID() const {
		return m_strID;
	}

public:
	void QueryStatement( const CString& sql ) { m_QueryStatement = sql; }
	void SetDSItem( DSItem* item ) { m_item = item; }
	DSItem* GetDSItem(){
		return m_item;
	}

private:
	DSItem* m_item;
	CString m_strID;
	CString m_QueryStatement;
};

class DataSourceManager
{
public:
	DataSourceManager() { m_CurDsi = 0; }
	~DataSourceManager() {
		if ( m_CurDsi ) delete m_CurDsi; 
	for(int i = 0; i < m_Items.GetSize(); i++)
		delete m_Items.GetAt(i);
	m_Items.RemoveAll();}

public:
	const DataSourceItem* CurrentDataSourceItem() const { return m_CurDsi; }
	const DataSourceItem* GetDSItemByPFID( const CString& pfid ) const;
	const DataSourceManager* GetDSMByPFID( const CString& pfid ) const;

public:
	void CurrentDataSourceItem( DataSourceItem* dsi ) { m_CurDsi = dsi; }
	void AddAttachDSM(DataSourceManager* pDSM){
		ASSERT(pDSM);
		m_Items.Add(pDSM);
	}

private:
	const DataSourceItem* GetItemAt( int index ) const {
		return m_Items.GetAt( index )->CurrentDataSourceItem(); 
	}
	int GetCount() const { return m_Items.GetSize(); }

private:
	DataSourceItem* m_CurDsi;
	CArray<DataSourceManager*,DataSourceManager*> m_Items;
};

#endif // !defined(AFX_DATASOURCEMANAGER_H__0EE8FB56_78AB_4573_90B5_27E4605BE132__INCLUDED_)
